package com.db.sevent;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;

import com.component.GetNewGroup;
import com.data.Data;
import com.db.bean.Work;
import com.db.service.GroupServer;
import com.db.service.WorkServer;
import com.view.P1;
import com.view.P2;

/**
* @author 作者:秋哲    E-mail:2810201146@qq.com:
* @version 创建时间：2020年5月29日 下午4:42:00
* @description: 
*/
public class WorkEvent {
	//点击组列表后加载作业列表
	public void getWorkList() {
		String GuiGroupTitle =(String) P1.instance.com01.getSelectedItem();
		WorkServer server = new WorkServer();
		server.getWorkList(GuiGroupTitle);
		DefaultListModel<String> model = server.updateDataWorkList();
		P1.instance.list01.setModel(model);
	}
	//点击创建作业
	public void insertWork() {
		GetNewGroup newGroupGUI = new GetNewGroup();
		newGroupGUI.setVisible(true);
		newGroupGUI.setTitle("新建作业");
		newGroupGUI.textField_1.setText("");
		newGroupGUI.btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String title= newGroupGUI.textField_1.getText();
				String msg=newGroupGUI.textArea.getText();
				String GuiGroupTitle=(String)P1.instance.com01.getSelectedItem();
				int insert=new WorkServer().insertWork(title, msg, GuiGroupTitle);
				if(insert==1) {
					System.out.println("OK");
					getWorkList();
				}else {
					System.out.println("失败");
				}
			}
		});
	}
	//点击删除作业
	public void deleteWork() {
		int index = P1.instance.list01.getSelectedIndex();
		int n = new WorkServer().deleteWork(index);
		if(n==1) {
			System.out.println("删除成功");
			getWorkList();
		}
	}
	//点击修改作业
	public void updateWorl() {
		int GuiIndex = P1.instance.list01.getSelectedIndex();
		Work work = Data.workList.get(GuiIndex);
		GetNewGroup newGroupGUI = new GetNewGroup();
		newGroupGUI.setVisible(true);
		newGroupGUI.setTitle("修改作业");
		newGroupGUI.textField_1.setText(work.getTitle());
		newGroupGUI.textArea.setText(work.getMsg());
		newGroupGUI.btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String newTitle = newGroupGUI.textField_1.getText();
				String newMsg = newGroupGUI.textArea.getText();
				int insert = new WorkServer().updateWorl(newTitle, newMsg, GuiIndex);
				if(insert==1) {
					System.out.println("修改成功");
					getWorkList();
				}else {
					System.out.println("修改失败："+newTitle);
				}
			}
		});
	}
	//点击发布作业
	public void issue() {
		int GuiIndex = P1.instance.list01.getSelectedIndex();
//		Work work = Data.workList.get(GuiIndex);
//		GetNewGroup newGroupGUI = new GetNewGroup();
//		newGroupGUI.setVisible(true);
//		newGroupGUI.setTitle("修改作业");
//		newGroupGUI.textField_1.setText(work.getTitle());
//		newGroupGUI.textArea.setText(work.getMsg());
//		newGroupGUI.btnNewButton.addActionListener(new ActionListener() {
//			@Override
//			public void actionPerformed(ActionEvent e) {
//				String newTitle = newGroupGUI.textField_1.getText();
//				String newMsg = newGroupGUI.textArea.getText();
//				int insert = new WorkServer().updateWorl(newTitle, newMsg, GuiIndex);
//				if(insert==1) {
//					System.out.println("修改成功");
//					getWorkList();
//				}else {
//					System.out.println("修改失败："+newTitle);
//				}
//			}
//		});
	}
	//点击列表
	@SuppressWarnings("unused")
	public void clickWorkList() {
		int GuiIndex = P1.instance.list01.getSelectedIndex();
		Work work = Data.workList.get(GuiIndex);
		String url = "\r\n上传地址：\r\n"+"http://47.100.95.58:8080/?id="+work.getId();
		P2.instance.text01.setText(work.getMsg()+url);
		new WorkUserEvent().upWorkUserList();
	}
}
